using UnityEngine;
using System.Collections;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;


public class SAActionManager : Singleton< SAActionManager > 
{
	public Dictionary< int , SAAction > actionDic = new Dictionary< int , SAAction >();

	void Awake ()
	{
		if ( mInstance == null )
		{
			mInstance = this;
			initConfig();
		}
		
		// init here ,
	}

	public SAAction getAction( int action )
	{
		if ( !actionDic.ContainsKey( action ) ) 
		{
			load( action );
		}
				
		return actionDic[ action ];
	}

	
	public void load( int actionID )
	{
		string path = "Actions/" + actionID.ToString() + ".actz" ;

		FileStream stream = File.Open( GameSetting.StreamingAssetsPath + path , FileMode.Open , FileAccess.Read );

		if ( stream == null )
		{
			return;
		}

		byte[] com = new byte[ stream.Length ];
		stream.Read( com , 0 , (int)stream.Length );
		byte[] dcom = GameDefine.DeCompress( com );

		int pos = 0;
		// action,

		SAAction saAction = new SAAction();

		saAction.actionID = actionID;
		saAction.actionNum = dcom[ pos ]; pos += 1;
		saAction.actionU = dcom[ pos ]; pos += 1;

		for ( int i = 0; i < saAction.actionNum ; i++ )
		{
			byte direction = dcom[ pos ]; pos += 1;
			byte action = dcom[ pos ]; pos += 1; 
			short time = BitConverter.ToInt16( dcom , pos ); pos += 2;
			byte fc = dcom[ pos ]; pos += 1;

			if ( saAction.actions[ direction ] == null )
			{
				saAction.actions[ direction ] = new SAAction.SAActionFrame[ (int)SAActionType.COUNT ];
			}

			saAction.actions[ direction ][ action ] = new SAAction.SAActionFrame();
			saAction.actions[ direction ][ action ].time = time;
			saAction.actions[ direction ][ action ].frameCount = fc;
			
			saAction.actions[ direction ][ action ].frames = new int[ fc ];

			for ( int j = 0 ; j < fc ; j++ )
			{
				int id1 = BitConverter.ToInt32( dcom , pos ); pos += 4;

				saAction.actions[ direction ][ action ].frames[ j ] = id1;
			}

		}

		actionDic.Add( saAction.actionID , saAction );
		Debug.Log( "load action " + saAction.actionID );

		// textures,

		short textureNum = BitConverter.ToInt16( dcom , pos ); pos += 2;

		for ( int i = 0; i < textureNum ; i++ )
		{
			int id1 = BitConverter.ToInt32( dcom , pos ); pos += 4;

			if ( id1 > 0 )
			{
				SATexture saTexture = SATextureConfig.instance.getTexture( id1 );
				
				byte[] data = new byte[ saTexture.w * saTexture.h ];
				for ( int j = 0 ; j < data.Length ; j++ )
				{
					data[ j ] = dcom[ j + pos ];
				}
				pos += data.Length;
				saTexture.initData();

				if ( !saTexture.hasData ) 
				{
					// error
					//if ( GameSetting.UseAsync ) 
					//{
					//	StartCoroutine( setColor( saTexture , data ) );
					//	saTexture.hasData = true;
					//}
					//else
					//{
						saTexture.setColor( data );
					//}
				}

			}
		}

		stream.Close();
	}


	IEnumerator setColor( SATexture texture , byte[] data )
	{
		texture.setColor( data );
		
		yield return 0;
	}


	public void initConfig()
	{
		//load( 100000 );
	}
	
	public void clearConfig()
	{
		actionDic.Clear();
	}

}


